package com.next

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR'])
@Transactional(readOnly = true)
class CompanyController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def springSecurityService

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond Company.list(params), model: [companyCount: Company.count()]
    }

    def show(Company company)
    {
        respond company
    }

    // def create()
    // {
    //     respond new Company(params)
    // }

    @Transactional
    def save(Company company)
    {
        if (company == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (company.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond company.errors, view: 'create'
            return
        }

        company.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'company.label', default: 'Company'), company.id])
                redirect company
            }
            '*' { respond company, [status: CREATED] }
        }
    }

    def edit(Company company)
    {
        def user = User.findByUsername(springSecurityService.getPrincipal().username)
        if (UserRole.findByUserAndRole(user, Role.findByAuthority("ROLE_ADMINISTRATOR")))
        {
            respond company, view: 'editAdministrator'
        }
        else
        {
            respond company, view: 'editCompanyAdministrator'
        }
    }

    @Transactional
    def update(Company company)
    {
        if (company == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (company.hasErrors())
        {
            if (params.flag == 'editAdministrator')
            {
                transactionStatus.setRollbackOnly()
                respond company.errors, view: 'editAdministrator'
                return
            }
            if (params.flag == 'editCompanyAdministrator')
            {
                transactionStatus.setRollbackOnly()
                respond company.errors, view: 'editCompanyAdministrator'
                return
            }
        }

        company.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'company.label', default: 'Company'), company.id])
                redirect company
            }
            '*' { respond company, [status: CREATED] }
        }
    }

    @Transactional
    def delete(Company company)
    {

        if (company == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        company.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'company.label', default: 'Company'), company.id])
                redirect company
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'company.label', default: 'Company'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
